package com.mdz.sbgl.Controller;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.XML;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Entity.Gsxxb;
import com.mdz.sbgl.Entity.Sbwhb;
import com.mdz.sbgl.Entity.Sbxxb;
import com.mdz.sbgl.Mapper.GsxxbMapper;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/sbgl/gsxxb")
public class GsxxbController {

    @Autowired
    private GsxxbMapper gsxxbMapper;
    @Autowired
    private HttpSession session;
    /*
    *   查询gsxxb表总所有的数据
     */
    @ResponseBody
    @RequestMapping(value = "/getgsxxb",method = RequestMethod.GET)
    public List<Gsxxb> getgsxxb(){
        return gsxxbMapper.selectList(null);
    }

    @ResponseBody
    @RequestMapping(value = "/getgsxxblist",method = RequestMethod.GET)
    public String getsblist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, @RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj,@RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        String userid=session.getAttribute("userid").toString();
        List<Gsxxb> ncList=new ArrayList<>();
        if (userid.equals("1")){
           ncList=gsxxbMapper.selectList(new QueryWrapper<Gsxxb>().like("gsid",cxtj).or().like("gs",cxtj));
        }else {
            System.out.println(userid);
            ncList=gsxxbMapper.selectList(new QueryWrapper<Gsxxb>().eq("gsid",gsid));
        }
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getgsxxbbygsid/{gsid}",method = RequestMethod.GET)
    public List<Gsxxb> getgsxxbbygsid(@PathVariable("gsid") String gsid){
        return gsxxbMapper.selectList(new QueryWrapper<Gsxxb>().eq("gsid",gsid));
    }

    @ResponseBody
    @RequestMapping(value = "/getgsmc/{gsid}",method = RequestMethod.GET)
    public String getgsmc(@PathVariable("gsid") String gsid){
        String gsmc = gsxxbMapper.selectList(new QueryWrapper<Gsxxb>().eq("gsid",gsid)).get(0).getGs();
        return gsmc;
    }

    @ResponseBody
    @RequestMapping(value = "/getgsmcbygsmc/{gsmc}",method = RequestMethod.GET)
    public List<Gsxxb> getgsmcbygsmc(@PathVariable("gsmc") String gsmc){
        List<Gsxxb> gsxxbs = gsxxbMapper.selectList(new QueryWrapper<Gsxxb>().eq("gs",gsmc));
        return gsxxbs;
    }

    @ResponseBody
    @RequestMapping(value = "/insertgsxxb",method = RequestMethod.POST)
    public int insertgsxxb(Gsxxb gsxxb){
        return gsxxbMapper.insert(gsxxb);
    }

    @ResponseBody
    @RequestMapping(value = "/updategsxxb/{gsid01}", method = RequestMethod.PUT)
    public Integer updategsxxb(Gsxxb gsxxb,@PathVariable("gsid01") String gsid01) {
        System.out.println(gsxxb.getGsid());
        return gsxxbMapper.update(gsxxb, new UpdateWrapper<Gsxxb>().eq("id", gsid01));
    }

}

